package com.xsz.oa.helper;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.xsz.oa.model.entity.sys.User;

/**
 * 
 * @author  xieshangzhen
 * @date	2015年11月29日
 */
public abstract class PasswordHelper {
	
	private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	
	public static void encryptPassword(User user) {
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		
		String newPassword = new SimpleHash(
				HASH_ALGORITHM,
				user.getPassword(),
				ByteSource.Util.bytes(user.getCredentialsSalt()),
				HASH_INTERATIONS).toHex();
		
		user.setPassword(newPassword);
	}
}
